package com.zejson.controller;

import com.zejson.entity.Carriers;
import com.zejson.service.CarriersService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.filter.HttpPutFormContentFilter;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

@Controller
@Scope("prototype")
@RequestMapping("carriers")

public class CarriersController {

    @RequestMapping("")
    public ModelAndView index()throws ClassNotFoundException{
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        CarriersService carriersService =  ((CarriersService)applicationContext.getBean("carriersService"));
        List carriers = carriersService.getList();

        ModelAndView model = new ModelAndView("templates/carriers/carriers");
        model.addObject("carriers",carriers);
        return model;
    }

    @RequestMapping("/add_carriers.do")
    public String addTruckTeam(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        ModelAndView model;
        if (request.getMethod().equals("POST")){
            String SendCompany = request.getParameter("SendCompany");
            String SenAddress = request.getParameter("SenAddress");
            String SendPhone = request.getParameter("SendPhone");
            String ReceiveCompany = request.getParameter("ReceiveCompany");
            String ReceiveAddress = request.getParameter("ReceiveAddress");
            String ReceivePhone = request.getParameter("ReceivePhone");
            //String LeaveDate = request.getParameter("LeaveDate");
            //String ReceiveDate = request.getParameter("ReceiveDate");
            String InsuranceCost = request.getParameter("InsuranceCost");
            String TransportCost = request.getParameter("TransportCost");
            String OtherCost = request.getParameter("OtherCost");
            //String TotalCost = request.getParameter("TotalCost");
            String Remark = request.getParameter("Remark");
            String Fk_UserID = request.getParameter("Fk_UserID");

            Carriers carriers = new Carriers();
            carriers.setSendcompany(SendCompany);
            carriers.setSendaddress(SenAddress);
            carriers.setSendphone(SendPhone);
            carriers.setReceivecompany(ReceiveCompany);
            carriers.setReceiveaddress(ReceiveAddress);
            carriers.setReceivephone(ReceivePhone);
            Date utilDate  =new Date();
            java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
            carriers.setLeavedate(sqlDate);
            carriers.setReceivedate(null);
            carriers.setFinishedstate((byte) 0);
            float InsuranceCost1 = (float) Integer.parseInt(InsuranceCost);
            carriers.setInsurancecost(InsuranceCost1);
            float TransportCost1 = (float) Integer.parseInt(TransportCost);
            carriers.setTransportcost(TransportCost1);
            float OtherCost1 = (float) Integer.parseInt(OtherCost);
            carriers.setOthercost(OtherCost1);
            float totalCost = InsuranceCost1 + TransportCost1 + OtherCost1;
            carriers.setTotalcost(totalCost);
            carriers.setRemark(Remark);
            carriers.setFkUserid(Integer.parseInt(Fk_UserID));
            carriers.setCheckintime(sqlDate);
            carriers.setIsdeleted((byte) 0);

            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
            CarriersService carriersService = ((CarriersService)applicationContext.getBean("carriersService"));
            carriersService.insertByAutoGeneratePK("CarriersMapper",carriers);
        }

        return "redirect:/carriers";
    }

    @RequestMapping("update_carriers")
    public String updateCarrier(HttpServletRequest request) throws ClassNotFoundException {

        int carriersId = Integer.parseInt(request.getParameter("carriersId"));
        String SendCompany = request.getParameter("TeamName");
        String SenAddress = request.getParameter("TeamName");
        String SendPhone = request.getParameter("TeamName");
        String ReceiveCompany = request.getParameter("TeamName");
        String ReceiveAddress = request.getParameter("TeamName");
        String ReceivePhone = request.getParameter("TeamName");
        //String LeaveDate = request.getParameter("TeamName");
        //String ReceiveDate = request.getParameter("TeamName");
        String InsuranceCost = request.getParameter("TeamName");
        String TransportCost = request.getParameter("TeamName");
        String OtherCost = request.getParameter("TeamName");
        //String TotalCost = request.getParameter("TeamName");
        String Remark = request.getParameter("TeamName");
        String Fk_UserID = request.getParameter("TeamName");

        Carriers carriers = new Carriers();
        carriers.setCarriersid(carriersId);
        carriers.setSendcompany(SendCompany);
        carriers.setSendaddress(SenAddress);
        carriers.setSendphone(SendPhone);
        carriers.setReceivecompany(ReceiveCompany);
        carriers.setReceiveaddress(ReceiveAddress);
        carriers.setReceivephone(ReceivePhone);
        Date utilDate  =new Date();
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        carriers.setLeavedate(sqlDate);
        carriers.setReceivedate(null);
        carriers.setFinishedstate((byte) 0);
        float InsuranceCost1 = (float) Integer.parseInt(InsuranceCost);
        carriers.setInsurancecost(InsuranceCost1);
        float TransportCost1 = (float) Integer.parseInt(TransportCost);
        carriers.setTransportcost(TransportCost1);
        float OtherCost1 = (float) Integer.parseInt(OtherCost);
        carriers.setOthercost(OtherCost1);
        float totalCost = InsuranceCost1 + TransportCost1 + OtherCost1;
        carriers.setTotalcost(totalCost);
        carriers.setRemark(Remark);
        carriers.setFkUserid(Integer.parseInt(Fk_UserID));
        carriers.setCheckintime(sqlDate);
        carriers.setIsdeleted((byte) 0);

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        CarriersService carriersService = ((CarriersService)applicationContext.getBean("carriersService"));
        carriersService.updateById("CarriersMapper",carriers);

        return "redirect:/carriers";
    }

    @RequestMapping("delete_carriers")
    public String deleteCarriers(HttpServletRequest request) throws ClassNotFoundException {

        int carriersId = Integer.parseInt(request.getParameter("carriersId"));

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        CarriersService carriersService = ((CarriersService)applicationContext.getBean("carriersService"));
        carriersService.deleteById("CarriersMapper", carriersId);
        return "redirect:/carriers";
    }
}
